<?php
declare (strict_types=1);

namespace app\api\controller\cart;

use app\api\controller\Base;
use app\BaseController;
use app\common\service\CartService;
use think\Request;

class Cart extends Base
{
    /**
     * Author:YangXinPeng
     * 加入购物车
     * @param Request $request
     * @return \think\response\Json
     */
    public function addCart(Request $request)
    {
        $params = $request->post();
        $params['uid'] = $request->uid;
        $info = CartService::create($params);
        return json($info);
    }

    /**
     * Author:YangXinPeng
     * 减少购物车数量
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function subCartNum(Request $request)
    {
        $params = $request->get();
        $params['uid'] = $request->uid;
        $info = CartService::reduceGoods($params);
        return json($info);
    }

    /**
     * Author:YangXinPeng
     * 增加购物车数量
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addCartNum(Request $request)
    {
        $params = $request->get();
        $params['uid'] = $request->uid;
        $info = CartService::addGoods($params);
        return json($info);
    }


    /**
     * Author:YangXinPeng
     * 展示购物车
     * @param Request $request
     * @return mixed|void
     */
    public function carts(Request $request)
    {
        $uid = $request->uid;
        if (empty($uid)) {
            return $this->result([],200,'success');
        }
        $data = CartService::getCarts($uid);
        return $this->res($data);
    }

    /**
     * Author:YangXinPeng
     * 根据ID查询购物车信息
     * @param Request $request
     * @return mixed
     */
    public function findCartInfo(Request $request)
    {
        $params = $request->get();
        $params['uid'] = $request->uid;
        $data = CartService::findCart($params);
        return $this->res($data);
    }
}
